package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.GroupMapper;
import com.group3.dao.StudentGroupInfoMapper;
import com.group3.dao.StudentGroupMapper;
import com.group3.domain.*;
import com.group3.exception.BusinessException;
import com.group3.exception.ExceptionEnum;
import com.group3.pojo.Page;
import com.group3.pojo.receive.GroupReceive;
import com.group3.pojo.receive.Students;
import com.group3.pojo.result.GroupResult;
import com.group3.service.AGroupService;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AGroupServiceImpl implements AGroupService {

    @Resource
    private GroupMapper groupMapper;
    @Resource
    private StudentGroupMapper studentGroupMapper;
    @Resource
    private StudentGroupInfoMapper studentGroupInfoMapper;

    @Override
    @Transactional
    public Result addGroup(String groupName, Integer statue) {

        Group group = new Group();
        group.setGroupName(groupName);
        group.setStatue(statue);

        int rs = groupMapper.insert(group);

        return rs > 0 ? ResultUtil.success("添加成功") : ResultUtil.error("添加失败");
    }

    @Override
    @Transactional
    public Result<List<GroupResult>> selectGroup(String groupName) {

        GroupExample groupExample = new GroupExample();
        if (groupName != null) {
            groupExample.createCriteria()
                    .andGroupNameLike("%" + (groupName) + "%");
        }

        List<Group> groups = groupMapper.selectByExample(groupExample);

        return ResultUtil.success("查询成功", groups);
    }

    @Override
    @Transactional
    public Result updateGroup(GroupReceive groupReceive) {

        Group group = new Group();
        GroupExample groupExample = new GroupExample();
        groupExample.createCriteria()
                .andGroupIdEqualTo(groupReceive.getGroupId());

        groupMapper.updateByExampleSelective(group, groupExample);

        return ResultUtil.success("查询成功", group);
    }

    @Override
    @Transactional
    public Result deleteGroup(Integer groupId) {

//  TODO   deleteGroup   待实现


        return ResultUtil.success();
    }

    @Override
    @Transactional
    public Result<List<StudentGroupInfo>> selectStudentByGroup(Integer groupId, String studentName, String studentId, Page page) {
//        根据小组id和条件查找小组内学生


        StudentGroupInfoExample studentGroupInfoExample = new StudentGroupInfoExample();

        StudentGroupInfoExample.Criteria criteria = studentGroupInfoExample.createCriteria();

        if (NotNull.judge(groupId)) {
            criteria.andGroupIdEqualTo(groupId);
        }
//   TODONE     根据小组id和条件查找小组内学生 是否模糊查询studentId
        if (NotNull.judge(studentId)) {
            criteria.andStudentIdEqualTo(studentId);
        }
        if (NotNull.judge(studentName)) {
            criteria.andStudentNameLike("%" + studentName + "%");
        }

//        1.先开启分页
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());//启动分页
//        2.执行SQL语句
        List<StudentGroupInfo> klassResults = studentGroupInfoMapper.selectByExample(studentGroupInfoExample);
//        3.获取分页信息
        PageInfo<StudentGroupInfo> studentGroupInfoPageInfo = new PageInfo<>(klassResults);//获取分页信息

        return ResultUtil.success("查询成功", klassResults, studentGroupInfoPageInfo.getTotal());
    }

    @Override
    @Transactional
    public Result addStudentsToGroup(Students students) {

        StudentGroupKey studentGroupKey = null;//new StudentGroupKey();
        for (String studentId : students.getStudentIds()) {
            studentGroupKey = new StudentGroupKey();

            //将数据赋值到对应实体
            studentGroupKey.setGroupId(students.getGroupId());
            studentGroupKey.setStudentId(studentId);

            int rs = studentGroupMapper.insert(studentGroupKey);
            if (rs < 1) {
                throw new BusinessException(ExceptionEnum.ERROR);
            }
        }

        return ResultUtil.success("添加成功");
    }

    @Override
    @Transactional
    public Result updateStudentsToGroup(Students students) {
//        根据小组id和将小组内学生移到另一个小组

//        原小组
        StudentGroupKey studentGroupKey = null;//new StudentGroupKey();
//        新小组
        StudentGroupKey studentGroupKey1 = null;//new StudentGroupKey();
        for (String studentId : students.getStudentIds()) {
            studentGroupKey = new StudentGroupKey();
            studentGroupKey1 = new StudentGroupKey();

            //将数据赋值到对应实体
//            设置原小组信息，用作条件
            studentGroupKey.setGroupId(students.getGroupId());
            studentGroupKey.setStudentId(studentId);

//            设置新小组信息，用作参数
            studentGroupKey1.setGroupId(students.getNewGroupId());
            studentGroupKey1.setStudentId(studentId);

            StudentGroupExample studentGroupExample = new StudentGroupExample();
            studentGroupExample.createCriteria()
                    .andGroupIdEqualTo(studentGroupKey.getGroupId())
                    .andStudentIdEqualTo(studentGroupKey.getStudentId());

            int rs = studentGroupMapper.updateByExample(studentGroupKey1, studentGroupExample);
            if (rs < 1) {
                throw new BusinessException(ExceptionEnum.ERROR);
            }
        }

        return ResultUtil.success("转移成功");
    }

    @Override
    @Transactional
    public Result deletStudentsToGroup(Students students) {

        StudentGroupKey studentGroupKey = null;//new StudentGroupKey();
        for (String studentId : students.getStudentIds()) {
            studentGroupKey = new StudentGroupKey();

            //将数据赋值到对应实体
            studentGroupKey.setGroupId(students.getGroupId());
            studentGroupKey.setStudentId(studentId);

            int rs = studentGroupMapper.deleteByPrimaryKey(studentGroupKey);
            if (rs < 1) {
                throw new BusinessException(ExceptionEnum.ERROR);
            }
        }

        return ResultUtil.success("删除成功");
    }

    @Override
    @Transactional
    public Result PutGroupOfLeader(Integer groupId, String studentId) {

        Group group = new Group();
        group.setGroupId(groupId);
        group.setGroupLeaderId(studentId);

        int rs = groupMapper.updateByPrimaryKeySelective(group);

        if (rs < 1) {
            throw new BusinessException(ExceptionEnum.ERROR);
        }

        return ResultUtil.success("修改成功");
    }
}
